package com.feature.shop.user.service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.feature.core.domain.PageVo;
import com.feature.core.mybatis.BaseService;
import com.feature.core.mybatis.SmartBaseMapper;
import com.feature.shop.user.api.request.ShopAuthListRequest;
import com.feature.shop.user.api.request.ShopAuthPageRequest;
import com.feature.shop.user.api.vo.ShopAuthVo;
import com.feature.shop.user.converter.ShopAuthConverter;
import com.feature.shop.user.mapper.ShopAuthMapper;
import com.feature.shop.user.model.entity.ShopAuthEntity;

/***
 * 权限表Service
 */
@Service
public class ShopAuthService extends BaseService<ShopAuthEntity> {
    @Autowired
    private ShopAuthMapper shopAuthMapper;

    @Autowired
    private ShopAuthConverter shopAuthConverter;

    @Override
    public SmartBaseMapper<ShopAuthEntity> getSmartBaseMapper() {
        return shopAuthMapper;
    }

    public PageVo<ShopAuthVo> findPageList(ShopAuthPageRequest req) {
        PageVo<ShopAuthEntity> pageData = shopAuthMapper.selectPageList(req.getCurPage(), req.getPageSize(), shopAuthConverter.pageRequestToQuery(req));
        PageVo<ShopAuthVo> vo = PageVo.of(pageData, shopAuthConverter::entityListToVO);
        return vo;

    }

    public List<ShopAuthVo> findList(ShopAuthListRequest req) {
        List<ShopAuthEntity> list = shopAuthMapper.selectList(shopAuthConverter.listRequestToQuery(req));
        return shopAuthConverter.entityListToVO(list);
    }

    @Override
    public Class<ShopAuthEntity> getEntityClass() {
        return ShopAuthEntity.class;
    }

    public List<ShopAuthEntity> getByPaths(Collection<String> paths) {
        LambdaQueryWrapper<ShopAuthEntity> queryWrapper = Wrappers.lambdaQuery(ShopAuthEntity.class).in(ShopAuthEntity::getPath, paths);
        List<ShopAuthEntity> authList = shopAuthMapper.selectList(queryWrapper);
        return authList;
    }

    public ShopAuthEntity getByPath(String path) {
        List<ShopAuthEntity> auths = getByPaths(Arrays.asList(path));
        return auths.isEmpty() ? null : auths.get(0);
    }

}
